package com.lfl.service.impl;

import com.lfl.mapper.CategoryMapper;
import com.lfl.pojo.Category;
import com.lfl.service.CategoryService;
import com.lfl.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author 叶星痕
 * @data 2024/8/8 14:13
 * 文件名 : CategoryServiceImpl
 * 描述 :
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    //TODO: 添加文章
    @Override
    public void add(Category category) {
        //TODO 补充属性值
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        // TODO 创建人id
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        category.setCreateUser(userId);

        categoryMapper.add(category);
    }

    //TODO: 查询文章列表
    @Override
    public List<Category> list() {
        //TODO 更具已登录用户id查询文章列表
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return categoryMapper.list(userId);
    }

    //TODO: 根据id查询文章分类
    @Override
    public Category findById(Integer id) {
        Category c =categoryMapper.findById(id);
        return c;
    }

    //TODO: 更新文章分类
    @Override
    public void update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public void delete(Integer id) {
        categoryMapper.delete(id);
    }
}
